#!/bin/sh

source scripts/common.sh

####################################################################################################
	
IM_VERSION='6.6.3-2'
IM_BASENAME="ImageMagick"
IM_ARCHIVE="${IM_BASENAME}-${IM_VERSION}.tar.gz"
IM_DIR="${IM_BASENAME}-${IM_VERSION}"

IM_URL="http://waix.dl.sourceforge.net/project/imagemagick/ImageMagick/00-6.6.3/${IM_ARCHIVE}"
IM_PREFIX="${THIRD_PARTY_PREFIX}/${IM_BASENAME}"
IM_CORE_LIB=libMagickCore.a
IM_WAND_LIB=libMagickWand.a
IM_CPP_LIB=libMagick++.a

####################################################################################################

function build_imagemagick()
{

target=$1

mkdir -p ${THIRD_PARTY_BUILD} || exit 1
pushd ${THIRD_PARTY_BUILD} || exit 1

report ""
report "Removing old build from '${IM_DIR}'..."
rm -fr $IM_DIR
rm -fr $IM_PREFIX/${target}

report "Preparing ${target} '${IM_BASENAME}'..."
fetch $IM_URL $IM_ARCHIVE

report "Configuring ${target} '${IM_BASENAME}'..."
report ""

pushd ${IM_DIR} || exit 1
mkdir -p ${target} || exit 1

./configure \
		CXXFLAGS="${SYSROOT} -arch ${target} -I/usr/include -I${THIRD_PARTY_PREFIX}/tiff/universal/include -I${THIRD_PARTY_PREFIX}/jpeg/universal/include -I${THIRD_PARTY_PREFIX}/libpng/universal/include -I${THIRD_PARTY_PREFIX}/freetype/universal/include " \
		CFLAGS="${SYSROOT} -arch ${target} -I/usr/include -I${THIRD_PARTY_PREFIX}/tiff/universal/include -I${THIRD_PARTY_PREFIX}/jpeg/universal/include -I${THIRD_PARTY_PREFIX}/libpng/universal/include -I${THIRD_PARTY_PREFIX}/freetype/universal/include " \
		LDFLAGS="-Wl,-syslibroot,${DEVSDK} -arch ${target} -L/usr/lib -L${THIRD_PARTY_PREFIX}/tiff/universal/lib -L${THIRD_PARTY_PREFIX}/jpeg/universal/lib -L${THIRD_PARTY_PREFIX}/libpng/universal/lib -L${THIRD_PARTY_PREFIX}/freetype/universal/lib -ltiff -ljpeg -lpng -lz -lfreetype " \
		--prefix=${IM_PREFIX}/${target} \
		--build=${target}-apple-darwin10.6 \
        --target=${target}-apple-darwin10.6 \
        --with-quantum-depth=16 \
        --enable-static=yes \
		--enable-shared=no \
        --disable-openmp \
        --disable-opencl \
        --without-modules \
        --without-x \
		--with-gs-font-dir="${THIRD_PARTY_PREFIX}/fonts" \
		--enable-hdri=yes \
		--without-perl \
		--without-openexr 

if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

report "Building ${target} '${IM_BASENAME}'..."

nice make -j4 || exit 1
nice make install || exit 1

report "Built ${target} '${IM_BASENAME}'"
report ""

popd
popd

}

####################################################################################################

build_for_snow_leopard
for target in 'i686' 'x86_64' 'ppc'
do
	report "Building '$target' ..."
	build_imagemagick $target
done

for lib in 'libMagickCore.a' 'libMagickWand.a' 'libMagick++.a'
do
	create_universal_lib ${IM_PREFIX} ${lib}
	create_intel_lib ${IM_PREFIX} ${lib}
	create_powerpc_lib ${IM_PREFIX} ${lib}
done

####################################################################################################

